package com.songshan.interceptor;

import com.songshan.dto.Result;
import com.songshan.exception.BusinessException;
import com.songshan.exception.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author feb
 * @date 2024/6/18
 * @Description: Controller异常拦截器
 */
@Order(1)
@RestControllerAdvice
public class ControllerExceptionAdvice {

  private Logger log = LoggerFactory.getLogger(this.getClass());

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 拦截所有异常
   */
  @ExceptionHandler
  public Result handleThrowable(Throwable e) {
    Result result = new Result();
    result.setCode(500);
    result.setMsg("后台处理错误：" + e.getMessage());
    log.error(ExceptionUtil.getExceptionInfo(e));
    return result;
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 拦截BusinessException异常
   */
  @ExceptionHandler(BusinessException.class)
  public Result handleGlobalException(BusinessException e) {
    return Result.failure(e);
  }
}
